//
// Created by Administrator on 2023/11/28.
//

#ifndef RTTHREAD_UPACKER_H
#define RTTHREAD_UPACKER_H
#include "stdint.h"
#include "esp_log.h"
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif

//使用动态内存
#define USE_DYNAMIC_MEM 0

#if USE_DYNAMIC_MEM
#define UP_MALLOC
#define UP_FREE
#endif

#define MAX_PACK_SIZE 512 //最长消息长度,最大可用14位即16384
#define STX_L 0X55         //数据包头

typedef void (*PACKER_CB)(uint8_t type, uint16_t seq, uint16_t cmd, const uint8_t *data, int len);
typedef int (*PACKER_SB)(uint8_t *d, uint16_t s);

typedef struct
{

#if !USE_DYNAMIC_MEM
    uint8_t data[MAX_PACK_SIZE]; //payload的内存
    uint8_t send_data[MAX_PACK_SIZE];//发送的数据
#else
    uint8_t *data; //用来做payload序列化的内存
#endif
    uint16_t cnt_send;  //frame数据发送cnt
    uint16_t cnt;  //frame数据接收cnt

    PACKER_CB cb;   //数据包处理回调
    PACKER_SB send; //数据发送回调

} upacker_inst;

typedef upacker_inst *upacker_inst_t;

int upacker_init(upacker_inst_t packer, PACKER_CB handle, PACKER_SB send);
void upacker_pack(upacker_inst_t packer, uint16_t seq, uint16_t cmd, uint8_t crypto, uint8_t type, const uint8_t *payload, int payload_len);
void upacker_unpack(upacker_inst_t packer, const uint8_t *buff, uint16_t size);

#ifdef __cplusplus
}
#endif


#endif //RTTHREAD_UPACKER_H
